package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.UserBean;
import com.project.service.ILogService;
import com.project.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
@RequestMapping("user")
@Api(tags = "用户")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ILogService logService;

    @ApiOperation(value = "登录用户")
    @RequestMapping("login")
    public UserBean login(String userName, String password, HttpServletRequest req) {
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(userName,password));
            //添加日志
            UserBean loginUser=(UserBean) subject.getPrincipal();
            logService.addLog(loginUser);
            req.getSession().setAttribute("loginUser",loginUser);
            return loginUser;
        }catch (Exception e){
            return null;
        }
    }

    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest req) {
        return (UserBean) req.getSession().getAttribute("loginUser");
    }

    @RequestMapping("findByItem")
    @ApiOperation(value = "查询所有用户")
    public IPage<UserBean> findByItem(Integer pageNO, String userName) {
        return userService.findByItem(pageNO, userName);
    }
    @RequestMapping("add")
    @ApiOperation(value = "添加用户")
    public String add(UserBean user,Integer[] roleIdArr){
        if (userService.add(user,roleIdArr)){
            return "ok";
        }else {
            return "no";
        }
    }
    @RequestMapping("del")
    @ApiOperation(value = "删除用户")
    public String del(Integer userId,HttpServletRequest req){
        UserBean loginUser = (UserBean) req.getSession().getAttribute("loginUser");
        if (Objects.equals(loginUser.getUserId(),userId)){
            return "no";
        }
        userService.del(userId);
        return "ok";
    }
    @RequestMapping("findById")
    @ApiOperation(value = "查询用户信息")
    public UserBean findById(Integer userId){
        return userService.findById(userId);
    }
    @RequestMapping("update")
    @ApiOperation(value = "修改用户角色")
    public String update(Integer userId,Integer[] roleIdArray){
        userService.update(userId,roleIdArray);
        return "ok";
    }
    @RequestMapping("updatePwd")
    @ApiOperation(value = "修改用户密码")
    public String updatePwd(String password, String oldPassword,HttpServletRequest req){
        UserBean loginUser = (UserBean) req.getSession().getAttribute("loginUser");
        boolean updatePwd = userService.updatePwd(loginUser.getUserId(), password, oldPassword);
        return updatePwd ? "ok" : "no";
    }

    @RequestMapping("logOut")
    public String logOut(HttpServletRequest req){
        req.getSession().removeAttribute("loginUser");
        return "ok";
    }
}
